using System;
using System.Threading;

using System.Drawing;
using System.Drawing.Imaging;

using System.Runtime.InteropServices;

namespace ArgoszLite
{
    public class CameraCapture
    {

        private Kamera[] cam = new Kamera[2];
        private Boolean done = true;
        private Bitmap[] img = new Bitmap[2];

        Settings setts = Settings.Instance;

        public CameraCapture() { }

        public void CloseEventListener()
        {
            done = false;
            int i;

            for (i = 0; i < setts.Camera_num; i++)
            Close(i);
            
        }

        public void Start(int id)
        {
            RotateFlipType rtf;
            cam[id] = new Kamera();
            if (id == 0)
            {
                rtf = RotateFlipType.RotateNoneFlipY;
            }
            else
            {
                rtf = RotateFlipType.RotateNoneFlipXY;
            }
            cam[id].Open(id, 30, 640, 480, rtf);
        }

        public void Start_capture()
        {
            while (done)
            {
                setpic();
                Thread.Sleep(setts.Camera_delay);
            }
        }
        public void Close(int id)
        {
            cam[id].Close();
            cam[id] = null;
        }

        public Bitmap GetCameraPic(int id)
        {
            cam[id].getpic(ref img[id]);
            return img[id];
        }

        private void setpic()
        {
            ImgStore imgStore = ImgStore.Instance;

            int i;
            
            for (i=0; i< setts.Camera_num;i++)
            cam[i].getpic(ref img[i]);
            
            for (i=0; i< setts.Camera_num;i++)
            imgStore[i] = (Bitmap)img[i].Clone();

            for (i = 0; i < setts.Camera_num; i++)
            img[i].Dispose();

            imgStore.isModded = true;
            
        }

    }
}
